package org.lendoo.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.entity.Category;
import org.lendoo.web.entity.Image;
import org.lendoo.web.enumeration.ServiceResultEnum;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.dto.RelCategoryAndImageUrlListDto;
import org.springframework.stereotype.Service;
import org.lendoo.web.mapper.RelCategoryImageMapper;
import org.lendoo.web.entity.RelCategoryImage;
import org.lendoo.web.service.RelCategoryImageService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * RelCategoryImageService接口的实现类
 */
@Slf4j
@Service
public class RelCategoryImageServiceImpl extends ServiceImpl<RelCategoryImageMapper, RelCategoryImage> implements RelCategoryImageService {

    @Resource
    private ServiceHelper serviceHelper;

    /**
     * 新增
     * @param relCategoryImage 分类和图片的关系表
     * @return 添加成功返回1，添加失败返回-1
     */
    @Override
    public ServiceResult<Boolean> add(RelCategoryImage relCategoryImage) {
        baseMapper.insert(relCategoryImage);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 批量导入
     * @param relCategoryImageList 分类和图片的关系表（列表）
     * @return 批量导入成功返回1，批量导入失败返回-1
     */
    @Override
    public ServiceResult<Boolean> batchAdd(List<RelCategoryImage> relCategoryImageList) {
        baseMapper.insertBatchSomeColumn(relCategoryImageList);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 根据id更新
     * @param relCategoryImage 分类和图片的关系表
     * @return 更新成功返回1，更新失败返回-1
     */
    @Override
    public ServiceResult<Boolean> updateForId(RelCategoryImage relCategoryImage) {
        baseMapper.updateById(relCategoryImage);
        return new ServiceResult<>(ServiceResultEnum.UPDATE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.UPDATE_SUCCESS.getMessage());
    }

    /**
     * 根据id删除
     * @param id 主键
     * @return 删除成功返回1，删除失败返回-1
     */
    @Override
    public ServiceResult<Boolean> deleteById(Serializable id) {
        baseMapper.deleteById(id);
        return new ServiceResult<>(ServiceResultEnum.DELETE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.DELETE_SUCCESS.getMessage());
    }

    /**
     * 根据id查询
     * @param id 主键
     * @return 返回查询结果
     */
    @Override
    public ServiceResult<RelCategoryImage> selectById(Serializable id) {
        RelCategoryImage relCategoryImage = baseMapper.selectById(id);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relCategoryImage, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据条件，分页查询，升序/降序排列
     * @param relCategoryImage 分类和图片的关系表
     * @param pageNo 页号
     * @param pageSize 每页行数
     * @return 返回查询结果列表
     */
    @Override
    public ServiceResult<PageInfo<RelCategoryImage>> page(RelCategoryImage relCategoryImage, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        LambdaQueryWrapper<RelCategoryImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<RelCategoryImage> relCategoryImageList = baseMapper.selectList(lambdaQueryWrapper);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), new PageInfo<>(relCategoryImageList), Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查询所有记录
     * @return
     */
    @Override
    public ServiceResult<List<RelCategoryImage>> findAll() {
        List<RelCategoryImage> relCategoryImageList = baseMapper.selectList(null);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relCategoryImageList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据分类的id查询分类信息和对应图片的url
     * @param categoryId
     * @return
     */
    @Override
    public ServiceResult<RelCategoryAndImageUrlListDto> findCategoryAndImageUrlById(Integer categoryId) {
        Category category = serviceHelper.getCategoryMapper().selectById(categoryId);
        List<Image> imageList = serviceHelper.getRelCategoryImageMapper().findByCategoryId(categoryId);

        RelCategoryAndImageUrlListDto relCategoryAndImageUrlListDto = new RelCategoryAndImageUrlListDto();
        relCategoryAndImageUrlListDto.setCategory(category);
        relCategoryAndImageUrlListDto.setImageList(imageList);

        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relCategoryAndImageUrlListDto, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }
}
